package com.qph.app.system.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qph.app.system.service.IPermService;
import com.qph.base.exception.BusinessException;
import com.qph.base.security.entity.Permission;
import com.qph.utils.base.StringUtil;

/**
 * 资源管理
 * @author Administrator
 *
 */
@Controller
@RequestMapping(value="/perm")
public class PermController {

	@Resource(name="permService")
	private IPermService permService;
	
	/**
	 * 打酱油~跳转资源列表页面
	 * @return
	 */
	@RequestMapping(value="/permList")
	@RequiresPermissions("Perm:query")
	public String permList(){
		return "/system/perm/permList";
	}
	
	/**
	 * ajax查询资源列表--无分页
	 * @param role
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/getPermList")
	@RequiresPermissions("Perm:query")
	public List<Permission> getPermList(Permission perm){
		
		return permService.getModelList(perm);
	}
	
	
	/**
	 * 菜单列表
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/getMenulList")
	public List<Permission> getMenulList() {
		return permService.getMenulList();
	}
	
	/**
	 * 打酱油~跳转到增加资源页面
	 * @return
	 */
	@RequestMapping(value="/addPerm")
	@RequiresPermissions("Perm:add")
	public String addPerm(){
		return "/system/perm/addPerm";
	}
	
	
	/**
	 * 保存资源
	 * @param role
	 * @return
	 * @throws BusinessException 
	 */
	@ResponseBody
	@RequestMapping(value="/savePerm")
	public JSONObject savePerm(Permission perm) throws BusinessException{
		boolean result = false;
		
		if (StringUtil.isEmpty(perm.getId())){
			result = permService.insert(perm);
		} else {
			result = permService.update(perm);
		}
		
		JSONObject json = new JSONObject();
		json.put("result", result);
		return json;
	}
	
	/**
	 * 删除资源
	 * @param role
	 * @return
	 * @throws BusinessException 
	 */
	@ResponseBody
	@RequestMapping(value="/delPerm")
	@RequiresPermissions("Perm:delete")
	public JSONObject delPerm(Permission perm) throws BusinessException{
		int delCount = permService.delete(perm.getId());
		JSONObject json = new JSONObject();
		json.put("result", delCount > 0);
		return json;
	}
	
	
	/**
	 * 修改资源
	 * @param perm
	 * @return
	 */
	@RequestMapping(value="/editPerm")
	@RequiresPermissions("Perm:update")
	public String editPerm(HttpServletRequest request, String id){
		
		Permission perm = permService.getModel(id);
		request.setAttribute("perm", perm);
		return "/system/perm/editPerm";
	}
}
